package com.tenton.service.Impl;

import com.tenton.mapper.UserMapper;
import com.tenton.pojo.User;
import com.tenton.service.UserService;
import com.tenton.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Date: 2021/1/18
 * @Author: Tenton
 * @Description:
 */
public class UserServiceImpl implements UserService {
    /**
     * 登录验证
     * @Author: Tenton
     * @param userName
     * @return
     */
    @Override
    public Boolean getAdmin(String userName, String password, String role) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.listUser();
        boolean flag = false;
        for (User user : users) {
            if (user.getUserName().equals(userName) && user.getPassword().equals(password) &&user.getRole().equals(role)){
                flag = true;
            }
        }
        return flag;
    }


    /**
     * 新增用户
     * @Author: Tenton
     * @param user
     * @return
     */
    @Override
    public int insertUser(User user) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int result = mapper.insertUser(user);
        return result;
    }

    /**
     * 删除用户
     * @Author: Tenton
     * @param id
     * @return
     */
    @Override
    public int deleteUser(int id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int result = mapper.deleteUser(id);
        return result;
    }

    /**
     * 修改用户
     * @Author: Tenton
     * @param user
     * @return
     */
    @Override
    public boolean updateUser(User user) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        boolean flag = mapper.updateUser(user);
        return flag;
    }

    /**
     * 根据Id查询用户
     * @Author: Tenton
     * @param id
     * @return
     */
    @Override
    public User getUserById(int id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserById(id);
        return user;
    }

    @Override
    public User getUser(String userName, String password) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUser(userName, password);
        return user;
    }

    /**
     * 获取所有用户信息
     * @Author: Tenton
     * @return
     */
    @Override
    public List<User> listUser() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.listUser();
        return users;
    }
}
